// packageA/pages/outInFenceCount/outInFenceCount.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
const util = require('../../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //列表
    list:[],
    //详情
    detailList:[],
    //无数据
    showNothing:false,
    //折叠面板当前值
    activeName: null,
    //日期
    showDate:false,
    currentDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    currentDateStr: util.timeFormatDay(new Date()),
    //明天
    tomorrow:null,
    //权限
    authority: null,
    //某一个员工的imei
    imeiParam:"",
    nickNameParam:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);
      this.setData({
        imeiParam: paramObj.imei,
        nickNameParam:paramObj.nickName
      })  
    }


    this.setData({	        
      authority: app.globalData.authority
    })

    //获取列表
    if (this.data.imeiParam==""){
      this.getList();
    }else{
      this.setData({
        list: [{imei:this.data.imeiParam,nickName:this.data.nickNameParam}]
      })
    }
    
  },

  //获取列表
  getList(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    //明天
    let t=util.timeFormatDay(util.countNextDay(new Date(this.data.currentDate),1));
    this.setData({
      tomorrow: t
    })

    let param={
      startTime: this.data.currentDateStr,
      endTime: this.data.currentDateStr
    }

    //员工查询
    let authority = this.data.authority;
    if(authority.manage.userCard.staff){
      let user = app.globalData.userInfo;
      param.imei = user.imei ? user.imei : null;
    }

    if (this.data.imeiParam!=""){
      param.imei=[this.data.imeiParam]
    }

    console.log(param)
    CommonService.outInFenceCount(param).then(res=>{
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;

        console.log('记录');
        console.log(result);

        this.setData({
          list: result
        })
        if(result.length==1){
          this.setData({
            activeName: result[0].imei,
          })

          let param={
            imeiArr: [result[0].imei],
            startTime: this.data.currentDateStr,
            endTime: this.data.currentDateStr
          }
          this.findOutInFenceDetail(param);
        }
      }else{
        this.setData({
          showNothing: true
        })
      }
    }).catch((error) => {
      this.setData({
        showNothing: true
      })
    })
  },

  //切换
  onChange(event) {
    this.setData({
      activeName: event.detail,
    })
  }, 

  //展开显示详情
  showDetail(event){
    console.log();
    let param={
      imeiArr: [event.detail],
      startTime: this.data.currentDateStr,
      endTime: this.data.currentDateStr
    }
    this.findOutInFenceDetail(param);
  },

  findOutInFenceDetail(param){
    
    CommonService.outInFenceRecord(param).then(res=>{
      Toast.clear();
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;

        console.log('详情');
        console.log(result);

        result.forEach(item => {
          if(item.inTime && item.inTime!='0001-01-01T00:00:00Z'){
            item.self_inTime = item.inTime.substring(11,19);
          }

          if(item.outTime && item.outTime!='0001-01-01T00:00:00Z'){
            item.self_outTime = item.outTime.substring(11,19);
          }

          if(item.intervals){
            item.self_intervals=util.secondToFormat(item.intervals/1000);
          }  
          
        });

        this.setData({
          detailList: result
        })
      }
    }).catch((error) => {
    })
  },

  //刷新列表
  refreshListAndPage(){
    this.setData({
      list: [],
      showNothing: false
    })

    this.getList();  
  },

  //显示日期
  onShowDate(){
    this.setData({
			showDate: true
    })
  },

  //隐藏日期
  onCloseDate(){
    this.setData({
			showDate: false
    })
  },

  //确认日期
  onConfirmDate(e){
    this.setData({
      currentDate: e.detail,
      currentDateStr: util.timeFormatDay(e.detail),
      activeName:null,
    })

    this.onCloseDate();

    //获取列表
    this.refreshListAndPage();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})